layui.define(function (exports) {
	var object = {
			c: {data:[]}, // 配置项
			f: function (c) { // 数据初始化
				var s=new Date().getTime(),t = this;
				t.c.id = c.id?c.id:'simple_table_role_auth';
				t.c.tableId = c.tableelem?c.tableelem:'table-'+s;
				//c.data = JSON.parse('[{"accountName":"费玉清","timeTypeName":"00:00~00:59","operatorName":"周杰伦"}]');
				t.c.data = c.data; // 数据
				console.log(c.data);
				t.c.roleId = c.roleId;
				t.c.typeArr = [{'name': '全部权限', 'value': 1},
				               {'name': '运营权限', 'value': 2},
				               {'name': '机构权限', 'value': 3},
				               {'name': '个人权限', 'value': 4}]
			},
	        load:function () { // 加载控件
	        	var h = ''
	        		+ '<div class="layui-form layui-form-item" lay-filter="' + this.id + '">'
		        		+ '<div class="layui-col-md12">'
		        			+ '<button type="button" class="layui-btn layui-btn-normal btn-save"><i class="layui-icon layui-icon-ok"></i> 保存</button>'
		        		+ '</div>'
		        		+ '<div style="width: 100%; overflow:auto; max-height:600px;">'
		        			+  this.loadTable()
        				+ '</div>'
	        		+ '</div>'
	            $('#' + this.c.id).html(h);
	            this.listenBtn();
	        },
	        loadTable:function () { // 加载表格
	            return '<table class="layui-table" style="width: 98%;margin:0;"><thead>'+this.loadTableOfThead()+'</thead><tbody>'+this.loadTableOfTbody()+'</tbody></table>';
	        },
	        loadTableOfThead:function () { // 加载表头
	            var h = '<tr style="background-color: #2e7fff; color: #fff;">';
	            h += '<th style="width:20%; text-align: center;">功能名称</th>';
	            h += '<th style="width:12%; text-align: center;">权限类型</th>';
	            h += '<th style="text-align: center;">按钮</th></tr>';
				return h;
	        },
	        loadTableOfTbody:function () { // 加载表tbody
	        	var t = this;
	        	var h = '<tbody>';
	            for(var i=0;i<t.c.data.length;i++){
	            	
		            h += '<tr>';
		            h += '<td style="text-align:left;"><div class="form-group"><label class="checkbox-inline margin_l50"><input type="checkbox" data-index="' + i + '" data-control="icheck" data-type="menu" data-check="' + t.c.data[i].selected + '" data-id="' + t.c.data[i].id + '"><span class="margin_l10">' + t.c.data[i].name + '</span></label></div></td>';
		            h += '<td style="text-align:left;">';
		            	h += '<form class="layui-form"><select data-menu-id="' + t.c.data[i].id + '" data-index="' + i + '" data-type="type" lay-filter="_type_filter_selector">';
		            	for (var j = 0; j < t.c.typeArr.length; j++) {
		            		h += '<option data-menu-id="' + t.c.data[i].id + '" value="' + t.c.typeArr[j].value + '" ' + (t.c.data[i].type == t.c.typeArr[j].value ? 'selected' : '') + '>' + t.c.typeArr[j].name + '</option>';
						}
	            		h += '</select></form>';
	            	h += '</td>';
		            
		            h += '<td style="text-align:left;">';
		            	h += '<div class="form-group">';
		            	for (var j = 0; j < t.c.data[i].buttonArr.length; j++) {
		            		h += '<label class="checkbox-inline layui-col-xs2"><input type="checkbox" data-control="icheck" data-index="' + i + '" data-type="button" data-check="' + t.c.data[i].buttonArr[j].selected + '" data-id="' + t.c.data[i].buttonArr[j].id + '"><span class="margin_l10">' + t.c.data[i].buttonArr[j].name + '</span></label>';
						}
		            	h += '</div>';
		            h += '</td>';
	                h += '</tr>';
	            }
                
	            h += '</tbody>';
				return h;
	        },
	        listenBtn:function () { // 添加监听

	            var t = this;
				layui.form.render('select');
				
	            // 监听“保存”按钮
	            $('#' + t.c.id).find('.btn-save').on('click',function (e) {
					$.ajax({
						type: "post",
						url: "/api/inter/ysRole/saveAuth",
						data: {
							roleId : t.c.roleId,
							menuArr: JSON.stringify(t.getData())
						},
						dataType: "json",
						success: function (result) {
							layer.msg(result.msg);
						},
						error: function () {
						    alert("加载失败！")
						}
					});
	            });
				
				// 绑定选择框的值变化提交事件
				layui.form.on('select(_type_filter_selector)', function (data) {
					console.log(data);
					var menuId = data.elem.attributes["data-menu-id"].value;
					var index = data.elem.attributes["data-index"].value;
					var type = data.value;
					t.c.data[index].type = type;
				});

	        	$("input[data-control='icheck']").each(function(index, dom){
	        		$(this).iCheck({
						labelHover : false, 
						cursor : true, 
						checkboxClass : 'icheckbox_square-blue', 
						radioClass : 'iradio_square-blue', 
						increaseArea : '20%' 
					});
	        	});

	        	$("input[data-check='true']").iCheck('check');
				
				$("input[data-control='icheck']").on('ifClicked', function(event){
					// 是否选中
					var isSelected = $(this).is(':checked') == true ? false : true;
					
					// 序号，用于定位同一行数据
					var index = $(this).attr('data-index');
					
					// 对应的ID值
					var id = $(this).attr('data-id');
					// 对应的权限类型
					var type = $('select[data-index=' + index + ']').val();
					
					if ($(this).attr('data-type') == 'menu') {
						t.c.data[index].selected = isSelected;
					} else {
		            	var buttonArr = t.c.data[index].buttonArr;
						for (var j = 0; j < buttonArr.length; j++) {
		            		if(buttonArr[j].id == id) {
		            			t.c.data[index].buttonArr[j].selected = isSelected;
		            		}
		            	}
					}
				});
	        },
	        reloadTable:function () { // 重新加载表格
	            $('#'+this.c.tableId).html(this.loadTable());
	            this.listenBtn();
	        },
	        getData:function () {
	            return this.c.data;
	        },
	        init:function (c) {this.f(c);this.load();return object;},
	        reload:function (c) {c.data = this.c.data;$('#' + this.c.id).empty();return this.init(c);},
	        addRow:function (row) {
	        	var c = this.c;
	        	c.data.push(row);
	        	$('#' + c.id).empty();
	        	return this.reload(c);
	        },
	        addRows:function (rows) {
	        	var c = this.c;
	        	for (var i = 0; i < rows.length; i++) {
		        	c.data.push(rows[i]);
				}
	        	$('#' + c.id).empty();
	        	return this.reload(c);
	        },
	        editRow:function (index, row) {
	        	var c = this.c;
				c.data[index] = row;
	        	$('#' + c.id).empty();
	        	return this.reload(c);
	        },
	        editRowOfAttr:function (index, row) {
	        	var c = this.c;
	        	for(var key in row){
	        		c.data[index][key] = row[key]
	        	}
	        	$('#' + c.id).empty();
	        	return this.reload(c);
	        },
	        reloadRows:function (rows) {
	        	var c = this.c;
	        	c.data = rows;
	        	return this.reload(c);
	        }
		};
	exports('simple_table_role_auth', object);
});